package com.sc.controller.property;

import cn.hutool.core.bean.BeanUtil;
import com.sc.domain.Repair;
import com.sc.dto.RepairDTO;
import com.sc.dto.RepairDateDTO;
import com.sc.dto.RepairPageDTO;
import com.sc.dto.ReviewDTO;
import com.sc.result.PageResult;
import com.sc.result.Result;
import com.sc.service.RepairService;
import com.sc.vo.RepairVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@Api(tags = "物业服务-报修管理")
@RestController
@RequestMapping("/property/repair")
@RequiredArgsConstructor
@CrossOrigin
public class RepairController {

    private final RepairService repairService;

    @ApiOperation("添加报修记录")
    @PostMapping("/add-repair")
    public Result addOne(@RequestBody RepairDTO repairDTO) {
        log.info("添加报修记录：{}",repairDTO);
        //拷贝数据
        Repair repair = BeanUtil.copyProperties(repairDTO, Repair.class);
        //维修状态
        repair.setStatus(2);
        //插入数据
        repairService.save(repair);
        return Result.success();
    }


    @ApiOperation("修改报修记录")
    @PutMapping("/modify-repair")
    public Result modifyRepair(@RequestBody RepairDTO repairDTO) {
        log.info("修改报修记录：{}",repairDTO);
        //拷贝数据
        Repair repair = BeanUtil.copyProperties(repairDTO, Repair.class);
        //维修状态
        repair.setStatus(2);
        //插入数据
        repairService.updateById(repair);
        return Result.success();
    }

    @ApiOperation("根据id删除一个维修记录")
    @DeleteMapping("/remove/{id}")
    public Result removeById(@PathVariable Integer id) {
        log.info("根据id删除维修记录：{}",id);
        repairService.removeById(id);
        return Result.success();
    }

    @ApiOperation("批量删除维修记录")
    @DeleteMapping("/remove-batch")
    public Result removeById(@RequestParam List<Integer> ids) {
        log.info("根据id批量删除维修记录：{}",ids);
        repairService.removeByIds(ids);
        return Result.success();
    }

    @ApiOperation("根据id获取维修记录")
    @GetMapping("/get/{id}")
    public Result<Repair> get(@PathVariable Integer id) {
        return Result.success(repairService.getById(id));
    }

    @ApiOperation("分页查询维修记录")
    @GetMapping("/query-page")
    public Result<PageResult<RepairVO>> pageQuery(RepairPageDTO repairPageDTO) {
        log.info("根据信息分页查询维修记录：{}",repairPageDTO);
        PageResult<RepairVO> result = repairService.pageQuery(repairPageDTO);
        return Result.success(result);
    }

    @ApiOperation("查询近期报修记录")
    @GetMapping("/query-all")
    public Result<List<RepairVO>> queryRepairData(RepairDateDTO repairDateDTO) {

        List<RepairVO> repairList = repairService.queryRepairData(repairDateDTO);

        return Result.success(repairList);
    }

    @PutMapping("/review-agree")
    @ApiOperation("审核状态流转-通过")
    public Result reviewAgree(@RequestBody ReviewDTO reviewDTO) {
        log.info("审核通过：{}",reviewDTO);
        //拷贝数据
        Repair repair = BeanUtil.copyProperties(reviewDTO, Repair.class);
        repair.setStatus(3);
        repairService.updateById(repair);
        return Result.success();
    }

    @PutMapping("/review-disagree")
    @ApiOperation("审核状态流转-不通过")
    public Result reviewDisagree(@RequestBody ReviewDTO reviewDTO) {
        log.info("审核不通过：{}",reviewDTO);
        //拷贝数据
        Repair repair = BeanUtil.copyProperties(reviewDTO, Repair.class);
        repair.setStatus(5);
        repairService.updateById(repair);
        return Result.success();
    }

    @PutMapping("/review-complete")
    @ApiOperation("审核状态流转-完成")
    public Result reviewComplete(@RequestBody ReviewDTO reviewDTO) {
        log.info("审核完成：{}",reviewDTO);
        //拷贝数据
        Repair repair = BeanUtil.copyProperties(reviewDTO, Repair.class);
        repair.setStatus(4);
        repairService.updateById(repair);
        return Result.success();
    }
}
